﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace WebShop.Infrastructure
{
    public static class UrlHelperExtension
    {
        #region controls
        /*public static MvcHtmlString Image(this HtmlHelper helper, string src, string altText, string className = "")
        {
            TagBuilder builder = new TagBuilder("img");
            builder.MergeAttribute("src", helper.Encode(src));
            builder.MergeAttribute("alt", helper.Encode(altText));
            builder.MergeAttribute("class", helper.Encode(className));

            return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
        }*/

        public static IHtmlString Image(this HtmlHelper helper, string name, string id)
        {
            return Image(helper, name, id, null);
        }

        public static IHtmlString Image(this HtmlHelper helper, string name, string id, object htmlAttributes)
        {
            var tagBuilder = new TagBuilder("img");
            UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);

            tagBuilder.Attributes["src"] = urlHelper.Action(name, null, new { id = id });
            tagBuilder.Attributes["alt"] = string.Format("{0} of {1}", name, id);
            tagBuilder.Attributes["type"] = "file";
            tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

            return MvcHtmlString.Create(tagBuilder.ToString());
        }

        public static IHtmlString ImageUpload(this HtmlHelper helper, string name, object htmlAttributes = null)
        {
            var tagBuilder = new TagBuilder("input");
            tagBuilder.GenerateId(name);
            UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);

            tagBuilder.Attributes["name"] = name;
            tagBuilder.Attributes["type"] = "file";

            tagBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

            return MvcHtmlString.Create(tagBuilder.ToString());
        }
        #endregion

        #region resources
        public static string Image(this UrlHelper helper, string image)
        {
            return helper.Content(string.Format("~/Content/Images/{0}", image));
        }

        public static string Script(this UrlHelper helper, string script)
        {
            return helper.Content(string.Format("~/Scripts/{0}", script));
        }

        public static string Css(this UrlHelper helper, string css)
        {
            return helper.Content(string.Format("~/Content/themes/base/{0}", css));
        }

        public static string DefaultCategoryImage(this UrlHelper helper)
        {
            return helper.Content("~/Content/themes/base/images/DefaultCategory.jpg");
        }
        #endregion

        #region navigation
        public static String ProductsList(this UrlHelper helper, int pageIndex = 1, int categoryID = 0, string q = "")
        {
            return helper.Action("Index", "Products", new { pageIndex = pageIndex, categoryID = categoryID, q });
        }
        public static String QuickSearch(this UrlHelper helper, string term)
        {
            return helper.Action("Index", "Products", new { q = term });
        }
        public static String DeleteProduct(this UrlHelper helper, int id)
        {
            return helper.Action("Delete", new { id = id });
        }
        #endregion
    }
}